/**
 *轻云蒜瓣已购产品CRM面板
 */
Ext.define('PRO.sale.view.SaleHeader', {
  extend: 'Ext.container.Container',
  border: 0,
  alias: 'widget.sale.saleheader',
  style: 'padding:0;background:"";margin-top:1px;',
  cls: 'pro-sale-header',
  initComponent() {
    const me = this;
    // 根据推送消息来刷新四连环头部的数据
    window._refersh_CRM_YXGL_XSSLH_ = function () {
      // 1.获得功能
      const CRM_YXGL_XSSLH = JE.getFunc('CRM_YXGL_XSSLH');
      if (JE.isNotEmpty(CRM_YXGL_XSSLH)) {
        const jemainview = CRM_YXGL_XSSLH.mainview;
        const CRM_YXGL_XSSLH_saleHandle = jemainview.down('[xtype=sale.saleheader]');
        const CRM_YXGL_XSSLH_saleHandleData = CRM_YXGL_XSSLH_saleHandle.getLoadData();
        const CRM_YXGL_XSSLH_data = {
          photo: me.buildUserPhoto(),
          userName: JE.USER.username,
          deptName: JE.USER.deptName,
          num: JE.isNotEmpty(CRM_YXGL_XSSLH_saleHandleData.news) ? CRM_YXGL_XSSLH_saleHandleData.news : 0,
          score: JE.isNotEmpty(CRM_YXGL_XSSLH_saleHandleData.soce) ? CRM_YXGL_XSSLH_saleHandleData.soce : 0,
          money: JE.isNotEmpty(CRM_YXGL_XSSLH_saleHandleData.bonus) ? CRM_YXGL_XSSLH_saleHandleData.bonus : 0,
        };
        CRM_YXGL_XSSLH_saleHandle.update(CRM_YXGL_XSSLH_data);
      }
    };


    /**
        window._refersh_sale_salegroup_ = function(slhId,sjId,sjname){
            if(JE.isNotEmpty(slhId) && JE.isNotEmpty(sjId) && JE.isNotEmpty(sjname)){
                var saleGroup = Ext.create('PRO.sale.view.SaleGroup',{backId:slhId,sjId:sjId,sjname:sjname});
                JE.win({
                title:'销售四连环团队组建',
                width:1000,
                height:600,
                items:saleGroup,
                autoShow:true
                });
            }

        } */


    const loadData = me.getLoadData();
    me.data = {
      photo: me.buildUserPhoto(),
      userName: JE.USER.username,
      deptName: JE.USER.deptName,
      num: JE.isNotEmpty(loadData.news) ? loadData.news : 0,
      score: JE.isNotEmpty(loadData.soce) ? loadData.soce : 0,
      money: JE.isNotEmpty(loadData.bonus) ? loadData.bonus : 0,
    };
    me.tpl = [
      '<div class="item">',
      '<div class="image">{photo}</div>',
      '<div class="info"><div>{userName}</div><div><span style="color:#386bd0;">{deptName}</span></div></div>',
      '</div>',
      '<div class="item loop" style="cursor:pointer;">',
      `<div class="image"><img src="${JE.buildCdnUrl('/pro/sale/images/slh-1.png')}"/></div>`,
      '<div class="info"><div>销售四连环</div><div><span style="color:#518cfb;">待处理 {num} </span>个</div></div>',
      '</div>',
      '<div class="item grade" style="cursor:pointer;">',
      `<div class="image"><img src="${JE.buildCdnUrl('/pro/sale/images/slh-3.png')}"/></div>`,
      '<div class="info"><div>四连环评分</div><div>平均分 <span style="color:#f37e9f;">{score}</span></div></div>',
      '</div>',
      '<div class="item bonus" style="cursor:pointer;">',
      `<div class="image"><img src="${JE.buildCdnUrl('/pro/sale/images/slh-2.png')}"/></div>`,
      '<div class="info"><div>四连环奖金</div><div>已获 <span style="color:#fbb149;">{money}</span> 元</div></div>',
      '</div>',
    ];
    me.listeners = {
      afterrender(box) {
        box.getEl().on('click', (e) => {
          if (e.getTarget('.loop', 4)) {
            JE.showFunc('V_CRM_YXGL_XSSLHRY', {
              type: 'grid',
              readOnly: true,
              whereSql: `${'and RY_BYQRID =' + "'"}${JE.USER.id}'` + 'and RY_YQZT_CODE in ("YQZ","BFP") and XSSLH_SLHZT_CODE="ZXZ"',
              orderSql: 'order by RY_YQSJ desc',
              j_query: {
                custom: [
                  { type: '=', code: 'RY_BYQRID', value: JE.USER.id },
                  { type: 'in', code: 'RY_YQZT_CODE', value: ['YQZ', 'BFP'] },
                  { type: '=', code: 'XSSLH_SLHZT_CODE', value: 'ZXZ' },
                ],
                order: [{ code: 'RY_YQSJ', type: 'desc' }],
              },
              callback(view) {
                const win = view.up('window');
                win.on('close', () => {
                  const loadData = me.getLoadData();
                  me.data = {
                    photo: me.buildUserPhoto(),
                    userName: JE.USER.username,
                    deptName: JE.USER.deptName,
                    num: JE.isNotEmpty(loadData.news) ? loadData.news : 0,
                    score: JE.isNotEmpty(loadData.soce) ? loadData.soce : 0,
                    money: JE.isNotEmpty(loadData.bonus) ? loadData.bonus : 0,
                  };
                  me.update(me.data);
                  const grid = me.up('panel[xtype=jemainview]').down('panel[xtype=jegridview]');
                  grid.store.reload();
                });
              },
            });
          } else if (e.getTarget('.grade', 4)) {
            JE.showFunc('V_CRM_YXGL_XSSLHPFJJMX', {
              type: 'grid',
              readOnly: true,
              whereSql: `${'and RY_CYRID like' + "'%"}${JE.USER.id}%'` + 'and RY_YQZT_CODE in ("YRL","YTC")',
              j_query: {
                custom: [
                  { type: 'like', code: 'RY_CYRID', value: `%${JE.USER.id}%` },
                  { type: 'in', code: 'RY_YQZT_CODE', value: ['YRL', 'YTC'] },
                ],
              },
            });
          } else if (e.getTarget('.bonus', 4)) {
            JE.showFunc('V_CRM_YXGL_XSSLHPFJJMX', {
              type: 'grid',
              whereSql: `${'and RY_CYRID like' + "'%"}${JE.USER.id}%'` + 'and RY_YQZT_CODE in ("YRL","YTC")',
              j_query: {
                custom: [
                  { type: 'like', code: 'RY_CYRID', value: `%${JE.USER.id}%` },
                  { type: 'in', code: 'RY_YQZT_CODE', value: ['YRL', 'YTC'] },
                ],
              },
            });
          }
        });
      },
    };
    me.createStyle();
    me.callParent(arguments);
  },
  /**
	 * 用户信息头像
	 */
  buildUserPhoto() {
    const photo = JE.getUserPhoto(false);
    if (JE.isNotEmpty(photo)) {
      return `<img class="photo photo-img" src="${photo}">`;
    }
    let name = JE.USER.username;
    name = name.length > 2 ? name.substring(name.length - 2) : name;
    return `<div class="photo photo-text">${name}</div>`;
  },
  createStyle() {
    const id = 'pro-sale-header';
    if (Ext.get(id)) { return; }
    const css = [
      '.pro-sale-header{padding: 15px 20px;background:#f0f2f5;}',
      '.pro-sale-header .item{width:25%;display:inline-block;text-align:center;padding:20px;background-color:#fff; position: relative;}',
      '.pro-sale-header .item .image{height:50px;width:50px;display:inline-block;}',
      '.pro-sale-header .item .image img,.pro-sale-header .item .image .photo{border-radius: 100%;height: 100%;width: 100%;display: table;}',
      '.pro-sale-header .item .image .photo{line-height: 50px;background-color: #386bd0;color: #fff;}',
      '.pro-sale-header .item .info{color:#5a5a5a;text-align: left;height: 50px;display: inline-block;line-height: 20px;padding: 5px 10px;vertical-align: bottom;}',
      '.pro-sale-header .item .info span{font-weight: bold;}',
      '.pro-sale-header .item:before{content: " ";height: 30px;width: 2px;background: #E6E5E2;position: absolute;right:0;top:50%;margin-top:-12px;}',
      '.pro-sale-header .item:first-child{border-radius: 4px 0 0 4px}',
      '.pro-sale-header .item:last-child{border-radius: 0 4px 4px 0}',
      '.pro-sale-header .item:last-child:before{display:none;}',
    ].join(' ');

    Ext.util.CSS.createStyleSheet(css, 'pro-sale-header');
  },
  getLoadData() {
    const obj = JE.ajax({
      url: '/je/product/crm/customerPortrit/ringNewsSoceBonus',
    });
    return obj;
  },
});
